package com.zzc.interview.model.vo;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.zzc.interview.model.dto.questionSubmit.JudgeInfo;
import com.zzc.interview.model.entity.QuestionSubmit;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;

@Data
public class QuestionSubmitVO implements Serializable {
    /**
     * id
     */
    private Long id;

    /**
     * 编程语言
     */
    private String language;

    /**
     * 用户提交代码
     */
    private String code;

    /**
     * 判题信息（json 数组）时间、空间消耗
     */
    private JudgeInfo judgeInfo;

    /**
     * 执行时间
     */
    private Long time;

    /**
     * 内存占用
     */
    private Long memory;

    /**
     * 运行结果，编译错误、运行错误、accepted。。。。
     */
    private String message;

    /**
     * 判题状态（0-待判题，1-判题中 2-成功 3-失败）
     */
    private Integer status;

    /**
     * 题目id
     */
    private Long questionId;

    /**
     * 提交该题的用户id
     */
    private Long userId;

    /**
     * 创建时间
     */
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 是否删除
     */
    private Byte isDelete;

    @TableField(exist = false)
    private UserVO userVO;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    public static QuestionSubmitVO objToVo(QuestionSubmit questionSubmit) {
        QuestionSubmitVO vo = new QuestionSubmitVO();
        BeanUtils.copyProperties(questionSubmit, vo);
        // judgeInfo
        JudgeInfo judgeInfo = JSONUtil.toBean(questionSubmit.getJudgeInfo(), JudgeInfo.class);
        vo.setJudgeInfo(judgeInfo);
        // 在界面方便显示
        vo.setTime(judgeInfo.getTime());
        vo.setMemory(judgeInfo.getMemory());
        vo.setMessage(judgeInfo.getMessage());
        return vo;
    }
}
